package cn.tb.org.service.impl;

import cn.tb.basic.service.impl.BaseServiceImpl;
import cn.tb.org.domain.Department;
import cn.tb.org.mapper.DepartmentMapper;
import cn.tb.org.service.IDepartmentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        this.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        String dirPath = "";
        if(department.getParent()==null){
            //顶级
            dirPath = "/"+department.getId();
        }else{
            //不是顶级，找到自己的上级，上级的dirPath+自己的id
            Department parent = departmentMapper.loadById(department.getParent().getId());
            if(parent!=null){
                dirPath = parent.getDirPath()+"/"+department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }


    @Override
    public List<Department> deptTree() {
        //一次性查询出所有数据，放到内存
        /*List<Department> departments = departmentMapper.loadAll();
        Map<Long, Department> allDepts = new HashMap<>();
        for (Department department : departments) {
            allDepts.put(department.getId(),department);
        }
        List<Department> firstDepts = new ArrayList<>();
        for (Department tmp : departments) {
            if(tmp.getParent_id()==null){
                //从所有数据中找出第一层级firstDepts，就是需要返回的最外层数据
                firstDepts.add(tmp);
            }else{
                //只要不是第一层级的，找到自己的上级，并将自己放入上级的children中
                Department parent = allDepts.get(tmp.getParent_id());
                if(parent!=null){
                   parent.getChildren().add(tmp);
                }
            }
        }
        return firstDepts;*/

        /*List<Department> departments = departmentMapper.loadAll();
        Map<Long, Department> allDepts = new HashMap<>();
        for (Department department : departments) {
            allDepts.put(department.getId(),department);
        }
        List<Department> firstDepts = new ArrayList<>();
        for (Department temp : departments) {
            if(temp.getParent_id()==null){
                //顶级
                firstDepts.add(temp);
            }else{
                //不是顶级，就找到自己的上级，把自己交给上级的children
                Department parent = allDepts.get(temp.getParent_id());
                parent.getChildren().add(temp);
            }
        }
        return firstDepts;*/
        //一次性查询到所有的数据，放在缓存中
        List<Department> departments = departmentMapper.loadAll();
        Map<Long, Department> allDepts = new HashMap<>();
        for (Department department : departments) {
            allDepts.put(department.getId(),department);
        }
        List<Department> firstDepts = new ArrayList<>();
        for (Department temp : departments) {
            if(temp.getParent_id()==null){
                //顶级
                firstDepts.add(temp);
            }else {
                //不是顶级，找到自己的上级，把自己交给上级的children
                Department parent = allDepts.get(temp.getParent_id());
                parent.getChildren().add(temp);
            }
        }
        return firstDepts;
    }
}
